/*
//  Copyright © 2014 Allen County Public Library.

  //        ACPL Mobile is free software: This program is distributed and you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.  ACPL Mobile is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY, including without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with ACPL Mobile. If not, see http://www.gnu.org/licenses/.
 * Author: James Harpe
 * 
 * 
 * Description: This activity displays a list of events generated by search or by location. Clicking on an event
 * 				launches the EventDetailActivity with the information associated with that particular event. 
 */

package us.in.lib.acpl.Mobile_ACPL;

import java.util.List;

import com.google.analytics.tracking.android.EasyTracker;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.LinearLayout;
import android.widget.AbsListView.OnScrollListener;
import us.in.lib.acpl.Mobile_ACPL.adapters.EventListAdapter;
import us.in.lib.acpl.Mobile_ACPL.context.Mobile_ACPL;
import us.in.lib.acpl.Mobile_ACPL.event.Event;
import us.in.lib.acpl.Mobile_ACPL.parents.BaseListActivity;
import us.in.lib.acpl.Mobile_ACPL.ws.EventHandler;

public class EventListActivity extends BaseListActivity implements
		OnScrollListener, OnItemClickListener {
	private EventHandler handler;
	private boolean readyForMore = true;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		this.getActionBar().setTitle("  Events");

		super.onCreate(savedInstanceState);
		setContentView(R.layout.event_list);

		handler = new EventHandler(this);
		Intent intent = getIntent();

		handler.getEvents(intent.getStringExtra("library"),
				intent.getStringExtra("term"));

	}
	  @Override
	  public void onStart() {
	    super.onStart();
	    EasyTracker.getInstance().activityStart(this); // Add this method.
	  }

	  @Override
	  public void onStop() {
	    super.onStop();
	    EasyTracker.getInstance().activityStop(this); // Add this method.
	  }

	public void setList(List<Event> list) {
		if (this.getListAdapter() == null) {

			LinearLayout footerView = (LinearLayout) this.getLayoutInflater()
					.inflate(R.layout.pending, null);
			this.getListView().addFooterView(footerView);
			this.findViewById(R.id.eventBar).setVisibility(View.GONE);
			this.setListAdapter(new EventListAdapter(this, list));
			this.getListView().setOnScrollListener(this);
			this.getListView().setOnItemClickListener(this);
			if (list.size() == 0) {
				findViewById(R.id.noEvents).setVisibility(View.VISIBLE);
			}else{
				findViewById(R.id.noEvents).setVisibility(View.GONE);
			}
		} else {

			readyForMore = true;
			this.getListView().findViewById(R.id.resultsBar)
					.setVisibility(View.GONE);
			EventListAdapter ad = (EventListAdapter) this.getListAdapter();
			ad.notifyDataSetChanged();
		}

	}

	@Override
	public void onScroll(AbsListView view, int firstVisibleItem,
			int visibleItemCount, int totalItemCount) {

		if (view.getAdapter() != null
				&& ((firstVisibleItem + visibleItemCount) >= totalItemCount)
				&& readyForMore) {

			readyForMore = false;
			if (handler.hasMoreResults()) {
				this.getListView().findViewById(R.id.resultsBar)
						.setVisibility(View.VISIBLE);

				handler.getMoreResults();
			}
		}
	}

	@Override
	public void onScrollStateChanged(AbsListView arg0, int arg1) {

	}

	@Override
	public void onItemClick(AdapterView<?> viewParent, View view, int pos,
			long id) {

		Event event = handler.getEventList().get(pos);

		Intent intent = new Intent(viewParent.getContext(),
				EventDetailActivity.class);
		intent.putExtra("title", event.getTitle());
		intent.putExtra("date", event.getStringDate());
		intent.putExtra("time", event.getTime());
		intent.putExtra("id", event.getId());
		intent.putExtra("library", event.getLibrary());
		intent.putExtra("location", event.getLocation());
		intent.putExtra("length", event.getLength());
		intent.putExtra("description", event.getDescription());
		intent.putExtra("ageGroups", event.getAgeGroupString());
		intent.putExtra("eventTypes", event.getEventTypes());
		intent.putExtra("otherInfo", event.getOtherInfo());
		

		startActivity(intent);
	}
	

}
